import sys
import numpy as np
import os
import re

def display(res_paths, eval_res_path, sm):
    colors = ["g", "b", "r"]
    markers = ["+", "p", "h", "o", "s", "<", ">", "1", "2", "3", "4", "H"]
    sum_ms = []
    import matplotlib.pyplot as plt
    plt.figure(figsize=(20,10),dpi=60)
    plt.rcParams['font.size'] = 12
    plt.grid(color='gray', linestyle='--', linewidth=1, alpha=0.5)
    
    for idx in range(len(res_paths)):
        res_path = res_paths[idx]
        color = colors[idx%len(colors)]
        meta_map = {}
        with open(res_path, 'r') as file:
            for line in file:
                items = line.split()
                # print(items)
                m, n, speedup = items[2], int(items[4]), float(items[14])
                if m not in meta_map:
                    meta_map[m] = ([], [])
                meta_map[m][0].append(n)
                meta_map[m][1].append(speedup)
        ms = list(meta_map.keys())
        for idx in range(len(ms)):
            x, y = np.array(meta_map[ms[idx]][0]), np.array(meta_map[ms[idx]][1])
            plt.plot(x, y, color=color, marker=markers[idx%len(markers)], linestyle='-')
        sum_ms += ms
    plt.plot(np.array(range(129)), np.array([1]*129), color=(0, 0, 0))
    plt.legend([f"m={int(int(m)/10000)}w" for m in sum_ms]+["speedup=1"])            
    plt.xlabel('n')
    plt.ylabel('speedup')
    plt.title(f"my/cublas {sm}")
    plt.savefig(eval_res_path)
    # plt.show()
    
if __name__ == "__main__":
    res_paths = [os.path.realpath(res_path) for res_path in sys.argv[1].split(":")]
    assert len(res_paths) != 0, f"{res_paths} is invalid"
    sm = sys.argv[2]
    eval_res_path = os.path.realpath(sys.argv[3])
    display(res_paths, eval_res_path, sm)
